package com.hyh.baowenlioaworking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hyh.baowenlioaworking.commom.Constants;
import com.hyh.baowenlioaworking.commom.R;
import com.hyh.baowenlioaworking.exception.CustomerException;
import com.hyh.baowenlioaworking.mapper.EmployeeMapper;
import com.hyh.baowenlioaworking.model.Employee;
import com.hyh.baowenlioaworking.model.Position;
import com.hyh.baowenlioaworking.model.dtos.EmployeeDto;
import com.hyh.baowenlioaworking.service.EmployeeService;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.TransactionIsolationLevel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.sql.SQLException;
import java.util.List;

/**
 * @BelongsPackage: com.hyh.baowenlioaworking.service.impl
 * @ClassName EmployeeServiceImpl
 * @Author: hyhWTX
 * @CreateTime: 2023-07-21  13:59
 * @Description: TODO (一句话描述以下该类的功能)
 * @Version: 1.0
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private SqlSession sqlSession;


    /**
     * 查询所有
     * @return
     */
    @Override
    public List<EmployeeDto> queryEmployee() {
        return employeeMapper.queryEmployee();
    }

    /**
     * 修改数据
     * @param employeeDto
     * @return
     */
    @Override
    public int updateEmployee(EmployeeDto employeeDto) {
        int i = employeeMapper.updateEmployee(employeeDto);
        if (i!=0){
            R.success(Constants.CODE_200,"修改成功！");
        }
        R.error(Constants.CODE_500,"修改失败！！！");
        return i;
    }

    @Override
    public R<Integer> deleteEmployeeById(int id) {
        if (id == 0) {
            return R.error(Constants.CODE_600, "id不能为0");
        } else {
            Integer res = employeeMapper.deleteById(id);
            if (res != 0) {
                return R.success(Constants.CODE_200, "删除成功！！！");
            } else {
                return R.error(Constants.CODE_404, "id没有找到！！！");
            }
        }
    }

    @Override
    public int deleteAll() {
        return employeeMapper.deleteAll();
    }

    @Override
    public R<String> addEmployee(EmployeeDto employeeDto) {
        String employeeName = employeeDto.getName();
        if (employeeName==null||employeeName.equals("") || employeeName.length() != 0){
            int i = employeeMapper.addEmployee(employeeDto);
            if (i!=0){
                return R.success(Constants.CODE_200,"添加成功");
            }
        }
        return R.error(Constants.CODE_405,"添加失败！！！");
    }


    @Override
    public Page<Employee> getEmployeesPage(Integer pageNum, Integer pageSize) {
        Page<Employee> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        // 可根据需要添加查询条件，如：
        // queryWrapper.eq("department_id", departmentId);
        // queryWrapper.like("name", keyword);
        return employeeMapper.selectPage(page, queryWrapper);
    }
}